<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增讲座报告')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <style>
        .form-group{
            flex: 0 0 50%;
            box-sizing: border-box;
        }
       .card{
           display: flex;
           flex-wrap: wrap;
       }
    </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-report-add">
        <div class="card">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">申请人：</label>
                <div class="col-sm-8">
<!--                    <input name="teacherName" class="form-control" type="text" required>-->
                    <input name="userId" class="form-control" type="hidden" th:value="*{sysUsers.userId}">
                    <input class="form-control" type="text" th:value="*{sysUsers.userName}" readonly>
<!--                    <select name="userId" class="form-control m-b" >-->
<!--                        <option th:each="user : ${sysUsers}" th:text="${user.userName}" th:value="${user.userId}" th:selected="${user.flag}"></option>-->
<!--                    </select>-->
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">讲座主题：</label>
                <div class="col-sm-8">
                    <input name="reportTheme" class="form-control" type="text" required maxlength="150">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">讲座题目：</label>
                <div class="col-sm-8">
                    <input name="reportSubject" class="form-control" type="text" required maxlength="150">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">主办单位：</label>
                <div class="col-sm-8">
                    <input name="hostUnit" class="form-control" type="text" required maxlength="50">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">讲座时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="reportTime" autocomplete="off" class="form-control" placeholder="yyyy-MM-dd-HH-mm" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                    <span id="timeRange" style="margin-top: 5px; display: block; color: #666;"></span>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">讲座地点：</label>
                <div class="col-sm-8">
                    <input name="reportPlace" class="form-control" type="text" required maxlength="50" />
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">参与人数：</label>
                <div class="col-sm-8">
                    <input name="reportNumber" class="form-control" type="text" maxlength="5">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">时长(分钟)：</label>
                <div class="col-sm-8">
                    <input name="reportDuration" class="form-control" type="text" maxlength="10">
                </div>
            </div>
            <div class="form-group row" >
                <label class="col-sm-3 control-label is-required">分类：</label>
                <div class="col-sm-8">
                    <select name="reportClassify" class="form-control m-b" required>
                        <option value="" selected>----请选择----</option>
                        <option th:each="reportIntegral : ${reportIntegralList}" th:text="${reportIntegral.classification}" th:value="${reportIntegral.id}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group row" >
            </div>

        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">报告资料：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="reportUrl">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="reportUrl" name="file" type="file">
                    </div>
                </div>
            </div>
        </div>


        </div>
    </form>
</div>
<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()">  </i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"> 关 闭
        </button>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/report"
    $("#form-report-add").validate({
        focusCleanup: true,
        rules: {
            reportNumber: {
                digits: true,     // 必须为整数
                min: 1
            },
            reportDuration: {
                required: true,    // 必填
                digits: true,     // 必须为整数
                min: 1           // 最小值为1
            }
        },
        messages: {
            reportNumber: {
                digits: "人数必须为整数",
                min: "参与人数必须大于0"
            },
            reportDuration: {
                required: "请输入报告时长",
                digits: "报告时长必须为整数",
                min: "报告时长必须大于0"
            }
        }
    });

    function submitHandler() {
        if ($.validate.form()) {
            if ($("input[name='reportUrl']").val() === "") {
                return $.modal.alertError("请上传附件");
            }
            // $.operate.saveTab(prefix + "/add", $('#form-report-add').serialize());
            // 新增：冲突校验
            checkTimePlaceConflict(function (hasConflict) {
                if (hasConflict) {
                    return $.modal.alertError("该场地在指定时间段已被占用，请调整时间或地点！");
                } else {
                    // 无冲突时提交表单
                    $.operate.saveTab(prefix + "/add", $('#form-report-add').serialize());
                }
            });
        }

    }

    // 新增：异步校验时间地点冲突
    function checkTimePlaceConflict(callback) {
        var reportTime = $("input[name='reportTime']").val(); // 讲座开始时间
        var hostUnit = $("input[name='hostUnit']").val(); // 主办单位
        var reportPlace = $("input[name='reportPlace']").val(); // 讲座地点
        var reportDuration = $("input[name='reportDuration']").val(); // 讲座时长
        if (!reportTime || !hostUnit || !reportPlace || !reportDuration) {
            callback(false); // 字段不全时跳过校验
            return;
        }

        $.ajax({
            url: prefix + "/checkConflict",
            type: "POST",
            data: {
                reportTime: reportTime,
                hostUnit: hostUnit,
                reportPlace: reportPlace,
                reportDuration: reportDuration
            },
            success: function(response) {
                console.log("后台场地校验后返回的结果：",response.data)
                callback(response.data.conflict); // 返回冲突结果
            },
            error: function() {
                $.modal.alertError("场地校验失败，请稍后重试");
                callback(true); // 失败时不允许提交
            }
        });
    }
    // $("input[name='reportTime']").datetimepicker({
    //     format: "yyyy-mm-dd",
    //     minView: "month",
    //     autoclose: true
    // });

    $("input[name='reportTime']").datetimepicker({
        format: "yyyy-mm-dd hh:ii",
        minView: "hour",
        autoclose: true
    });

    $(".file-upload").fileinput({
        uploadUrl: ctx + 'common/upload/jzbg', // 文件上传的URL
        initialPreviewAsData: true,
        maxFileCount: 1,
        maxFileSize: 51200, // 最多允许上传1个文件
        autoReplace: true, // 自动替换已上传的文件
        allowedFileExtensions: ["pdf"], // 只允许上传PDF文件
        slugCallback: function (filename) {
            var reportSubject = $("input[name='reportSubject']").val();
            // 自定义文件名生成规则
            if (reportSubject === "") {
                $(".file-uploada").fileinput('clear'); // 清空文件选择
                $.modal.alertError("请先填写讲座题目"); // 弹出错误提示
                return false; // 阻止上传
            }
            // 自定义文件名生成规则
            var trimmedSubject = reportSubject.substring(0, 150)
            fileExtension = filename.split('.').pop(); // 获取文件扩展名
            const year = new Date().getFullYear(); // 获取当前年份
            const newName = year + "-" + trimmedSubject + "-" + "报告资料" + "." + fileExtension; // 生成新文件名
            return newName; // 返回新文件名
        },
    }).on('fileuploaded', function (event, data, previewId, index) {
        // 文件上传成功后的回调
        $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
    }).on('fileremoved', function (event, id, index) {
        // 文件被移除后的回调
        $("input[name='" + event.currentTarget.id + "']").val('')
    })

</script>
</body>
</html>